<?php
namespace app\pay\controller\fumin;

use think\Controller;
use pay\fumin\RequestHandler;
use pay\fumin\Config;

class Base extends Controller {
    /**
    * [富民]支付接口中转
    */
    private $reqHandler;
    private $cfg;
    
    public function _initialize() {
        $this->reqHandler = new RequestHandler();
        $this->cfg = new Config();
    }
    
    /*
     * 畅远签名验证
     */
    public function changyuanSign($list) {
        ksort($list);
        $md5str = '';
        foreach ($list as $key => $val) {
            if(!empty($val)){
                $md5str = $md5str . $key . '=' . $val . '&';
            }
        }
        $md5key = model('PayBis')->get(['merchant_id' => $list['mchntId']])->md5key;
        $sign = md5($md5str . 'key=' . $md5key);
        return $sign;
    }
       
    /*
     * 组装数据并向富民发起post请求
     */
    public function reqFuminData($data, $url) {
        // 去除畅远分配的商户号、签名，采用富民分配的商户号
        $mchntId = $data['mchntId'];
        unset($data['mchntId']); 
        unset($data['signature']); 
        $data['mchntId'] =  $this->cfg->C('mchntId'); // 必填项，商户号，由支付平台分配
        $data['version'] = $this->cfg->C('version'); // 接口版本号
        $data['signature'] = $this->reqHandler->createSign($data); // 创建签名
        $encodeData = $this->reqHandler->rsaEncrypt($data['signature'], $data); // rsa加密
        $result = $this->reqHandler->curl($url, $encodeData);
        $result['mchntId'] = $mchntId; // 返回时使用畅远分配的商户号
        return json_encode($result, 320);
    }
}

